#!/bin/bash
# 批量设置 Notepad-- 为默认文本编辑器

echo "=========================================="
echo "  设置 Notepad-- 为默认文本编辑器"
echo "=========================================="
echo ""

# 检查 xdg-mime 命令是否可用
if ! command -v xdg-mime &> /dev/null; then
    echo "✗ 错误：未找到 xdg-mime 命令"
    echo "  请安装 xdg-utils 包"
    exit 1
fi

# 定义要关联的 MIME 类型
MIME_TYPES=(
    # 纯文本
    "text/plain"
    "text/english"
    
    # 编程语言
    "text/x-c"
    "text/x-chdr"
    "text/x-c++src"
    "text/x-c++hdr"
    "text/x-csrc"
    "text/x-java"
    "text/x-python"
    "application/x-python"
    "text/x-perl"
    "application/x-perl"
    "application/x-ruby"
    "text/x-lua"
    "application/x-lua"
    "application/x-php"
    "text/x-php"
    
    # Shell 脚本
    "application/x-shellscript"
    "text/x-sh"
    "application/x-sh"
    "text/x-bash"
    "application/x-bash"
    "application/x-zsh"
    
    # Web 开发
    "text/html"
    "text/css"
    "application/javascript"
    "text/javascript"
    "application/json"
    "application/xml"
    "text/xml"
    "image/svg+xml"
    
    # 标记语言
    "text/markdown"
    "text/x-markdown"
    "text/x-yaml"
    "application/x-yaml"
    "text/yaml"
    "application/yaml"
    
    # 配置文件
    "text/x-ini"
    "application/x-wine-extension-ini"
    "text/x-properties"
    "application/x-java-properties"
    "text/x-config"
    "application/toml"
    "text/x-toml"
    
    # 构建文件
    "text/x-makefile"
    "text/x-cmake"
    
    # 其他
    "text/x-patch"
    "text/x-diff"
    "text/x-log"
    "application/x-desktop"
)

echo "将为以下文件类型设置 Notepad-- 为默认编辑器："
echo ""

SUCCESS_COUNT=0
FAIL_COUNT=0

for mime in "${MIME_TYPES[@]}"; do
    if xdg-mime default NotePad--.desktop "$mime" 2>/dev/null; then
        echo "  ✓ $mime"
        ((SUCCESS_COUNT++))
    else
        echo "  ✗ $mime (失败)"
        ((FAIL_COUNT++))
    fi
done

echo ""
echo "=========================================="
echo "  完成"
echo "=========================================="
echo ""
echo "  成功：$SUCCESS_COUNT 个 MIME 类型"
if [ $FAIL_COUNT -gt 0 ]; then
    echo "  失败：$FAIL_COUNT 个 MIME 类型"
fi
echo ""

# 验证几个重要的 MIME 类型
echo "验证主要文件类型关联："
for mime in "text/plain" "application/json" "text/x-python" "text/x-shellscript"; do
    DEFAULT=$(xdg-mime query default "$mime" 2>/dev/null)
    if [ "$DEFAULT" = "NotePad--.desktop" ]; then
        echo "  ✓ $mime → NotePad--"
    else
        echo "  ⚠ $mime → $DEFAULT (未设置为 NotePad--)"
    fi
done

echo ""
echo "提示："
echo "  • 某些应用程序可能需要重启才能识别新的默认程序"
echo "  • 您可以在文件管理器中右键文件 → 属性 → 打开方式 来验证"
echo "  • 要恢复系统默认，请编辑 ~/.config/mimeapps.list"
echo ""

